;BYSTRETKA ROSLINOZERNA
;ZYWI SIE FOOD musi jesc zeby przezyc
;NISZCZY DREZWA I DOSTAJE ZA TO PUNKTY 
;UCIEKA PRZED DRAPIEZNIKAMI

; najważniejsze jest przezycie ()czyli jedzenie i ucieczka przed drapierznikami)
; kolejne jest zjadanie drzew dostaje za to punkty
;(clear)
;przyjmuje
(deftemplate visible (slot Object) (slot PosX) (slot PosY)) ;symbol obiektów (agentów)
(deftemplate field (slot Type) (slot Resource) (slot PosX) (slot PosY)) ;symbol pól	
(deftemplate stat (slot Name) (slot Value))	
;zwraca
(deftemplate move (slot Move) (slot MoveX) (slot MoveY))

;moje deftemplate
(deftemplate poleBystretki (slot nazwaPola)(slot wartoscPola))
(deftemplate niebezpieczenstwo (slot PosX)(slot PosY))

;===============================================================
;sprawdzam czy bystretka jest glodna ()jest glodna gdy ma food mniej niz 15)

(defrule jestemGlodny
    (stat (Name food) (Value ?v) )
    (test (< ?v 15))
    =>
    (assert (jestemGlodny))
    )

;Sprawda na czym stoi bystretka
(defrule naCzymStoi
    (field(Type ?typPola)(Resource ?ileJednostek)(PosX 0)(PosY 0))
    (test (> ?ileJednostek 0))
    =>
    (assert (poleBystretki(nazwaPola ?typPola)(wartoscPola ?ileJednostek)))
)  

;sprawdza czy jest bezpiecznie (sprawdza gdzie jest drapieznik)
(defrule czyJestBezpiecznie
    (visible (Object DRAPIEZNIK)(PosX ?x)(PosY ?y))
    =>
    (assert (niebezpieczenstwo(PosX ?x)(PosY ?y)))
    )

(defrule jedzenie
    (declare (salience 50))
    (and(jestemGlodny))
    (poleBystretki(nazwaPola FOOD)(wartoscPola ?r))
    =>
    (assert (move (Move collectFood) (MoveX 0) (MoveY 0)))
)

(defrule idzDoJedzenia
    (declare (salience 40))
    (field (Type FOOD) (Resource ?r) (PosX ?x) (PosY ?y))
    (jestemGlodny)
;    (not
;        (exists
;            (field (Type FOOD) (Resource ?newR) (PosX ?newX) (PosY ?newY))
;            (test (> ?newR ?r))
;            )
;        )
    =>
    (if (< ?x 0) then
        (assert  (move (Move move) (MoveX -1) (MoveY 0)))
        else (if (> ?x 0) then
            (assert  (move (Move move) (MoveX 1) (MoveY 0)))
            
        else (if (> ?y 0) then
            (assert  (move (Move move) (MoveX 0) (MoveY 1)))
            
        else (if (< ?y 0) then
            (assert  (move (Move move) (MoveX 0) (MoveY -1)))))))
)
  

;ucieka gdy jest w niebezpieczenstwie
(defrule uciekaj
    (declare (salience 20))
    (niebezpieczenstwo(PosX ?x)(PosY ?y))
    ;(not
    ;    (exists
     ;       (niebezpieczenstwo(PosX ?x2)(PosY ?y2))
    ;		(test (> (+(abs (- 0 ?x)) (abs (- 0 ?y))) (+(abs (- 0 ?x2)) (abs (- 0 ?y2)))))
	 ;  	 )) 
  	
=>
   
    (if (and (= ?x 0) (= ?y 1)) then
        (assert  (move (Move move) (MoveX 0) (MoveY -1)))
        else (if (and (= ?x 0) (= ?y -1)) then
            (assert  (move (Move move) (MoveX 0) (MoveY 1)))
            
        else (if (and (= ?x 1) (= ?y 0)) then
            (assert  (move (Move move) (MoveX -1) (MoveY 0)))
            
        else (if (and (= ?x -1) (= ?y 0)) then
            (assert  (move (Move move) (MoveX 1) (MoveY 0)))
            ))))
)


   

(defrule zetnijDrzewo
    (declare (salience 10))
    ;(field (Type TREE)(Resource ?r)(PosX ?x)(PosY ?y))
    (poleBystretki(nazwaPola TREE))
    =>
   (assert (move (Move cutTree) (MoveX 0) (MoveY 0)))
    (printout t scina_drzewo crlf) 
)


(defrule idzDoDrzewa
    (declare (salience 5))
    (field (Type TREE) (Resource ?r) (PosX ?x) (PosY ?y))
    (test(> ?r 0))
;    (not
;        (exists
;            (field (Type FOOD) (Resource ?newR) (PosX ?newX) (PosY ?newY))
;            (test (> ?newR ?r))
;            )
;        )
    =>
    		 (if (< ?x 0) then
        	(assert  (move (Move move) (MoveX -1) (MoveY 0)))
        else (if (> ?x 0) then
            (assert  (move (Move move) (MoveX 1) (MoveY 0)))
        else (if (> ?y 0) then
            (assert  (move (Move move) (MoveX 0) (MoveY 1)))
        else (if (< ?y 0) then
            (assert  (move (Move move) (MoveX 0) (MoveY -1)))))))

    
)

